﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Services_getSamplePointsAndYield : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.Charset = "utf-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        string imageId = Request.QueryString[0].ToString().Trim();
        string rootId = Request.QueryString[1].ToString().Trim();//选择的省或直辖市编号
        string distId = Request.QueryString[2].ToString().Trim();//选择的二级城市编号

        string result = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "\n";
        string SP_FILE_NAME = "";
        string DistrictId = "";

        string[] ImageIdArray = imageId.Split('_');
        string samplingDateString = ImageIdArray[2] + "-" + ImageIdArray[3] + "-" + ImageIdArray[4];//选中的影像的日期
       
        string queryString = "select SP_FILE_NAME,DISTID from RS.DISTRICT_IMAGE_SP_REF where IMAGEID = '" + imageId + "'";
        DataSet ds = OracleDataBase.Query(queryString);
        DataTable dt = ds.Tables[0];
        if (dt.Rows.Count == 1)
        {
            SP_FILE_NAME = dt.Rows[0][0].ToString();
            DistrictId = dt.Rows[0][1].ToString();
        }
        //string IMAGE_NAME = dt.Rows[0][2].ToString();
        dt.Dispose();
        ds.Dispose();

        if (Convert.ToInt32(DistrictId) > 50)//判断该行政区是否是省级
        {
            queryString = "select POINTNAME,JIN,WEI from rs." + SP_FILE_NAME + " where distid = '" + DistrictId + "'";
        }
        else
        {
            queryString = "select POINTNAME,JIN,WEI from rs." + SP_FILE_NAME + " where rootid = '" + DistrictId + "'";
        }
        ds = OracleDataBase.Query(queryString);
        dt = ds.Tables[0];

        queryString = "select POINTNAME,LAI,MCL,SLFW,PLANTGDW FROM RS.SP_AGRI_PARAM WHERE SAMPDATE = to_date('" + samplingDateString + "','yyyy-mm-dd')";//'2005-1-1','yyyy-mm-dd'.ToShortDateString() + "'";
        DataSet ds1 = OracleDataBase.Query(queryString);
        DataTable dt1 = ds1.Tables[0];

        //10-31日添加---->
        DataTable dt_yield=new DataTable();
        if (distId != "" && distId != "1")
        {
            queryString = "select POINTNAME,yeild from sp_yeild_param where distid=" + distId + " and sampdate=to_date('" + samplingDateString + "','yyyy-mm-dd')";
            DataSet ds_yield = OracleDataBase.Query(queryString);
            dt_yield = ds_yield.Tables[0];            
            
        }
        if(distId=="1")
        {
            //当二级城市选中的是所有区县时，按选中的省、直辖市的编号查询数据库
            queryString = "select POINTNAME,yeild from sp_yeild_param where rootid=" + rootId + " and sampdate=to_date('" + samplingDateString + "','yyyy-mm-dd')";
            DataSet ds_yield = OracleDataBase.Query(queryString);
            dt_yield = ds_yield.Tables[0];           
            
        }
        //<----10-31日添加
        //将样点文件输出到XML文档
        result += "<SamplePoints>" + "\n";

        if (dt.Rows.Count > 0 && dt.Rows.Count == dt1.Rows.Count)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int k = 0; k < dt.Rows.Count; k++)
                {
                    if (dt1.Rows[i][0].ToString() == dt.Rows[k][0].ToString())
                    {
                        result += "<Point name = '" + dt.Rows[k][0].ToString() + "' Lon = '" + dt.Rows[k][1].ToString() + "' Lat = '" + dt.Rows[k][2].ToString() + "'";

                    }
                }
                //for (int j = 0; j < dt1.Rows.Count; j++)
                //{
                //    if (dt1.Rows[j][0].ToString().ToUpper() == dt.Rows[i][0].ToString().ToUpper())
                //    {
                //        result += " LAI = '" + dt1.Rows[i][1].ToString() + "' MCL = '" + dt1.Rows[i][2].ToString() + "' SLFW = '" + dt1.Rows[i][3].ToString() + "' PLANTGDW = '" + dt1.Rows[i][4].ToString() + "'/>" + "\n";
                //        break;
                //    }
                //}
                //将单产数据添加到节点的YEILD属性
                if (dt_yield.Rows.Count > 0)
                {
                    foreach (DataRow row in dt_yield.Rows)
                    {
                        //string a = row["POINTNAME"].ToString();
                        //string b = dt.Rows[i][0].ToString();
                        if (row["POINTNAME"].ToString() == dt.Rows[i][0].ToString())
                        {
                            result += " YIELD ='" + row["yeild"] +" (kg)" +"'/>" + "\n";
                        }
                        
                    }
                }
            }
        }
        result += "</SamplePoints>";
        Response.Write(result);
    }
}
